//
//  MLSegmentViewController.m
//  MLSegmentBar
//
//  Created by dreamforstar on 2018/4/22.
//  Copyright © 2018年 com.jiankeyan. All rights reserved.
//

#import "MLSegmentViewController.h"

@interface MLSegmentViewController ()<MLSegmentBarDelegate,UIScrollViewDelegate>
/** <#name#>*/
@property (nonatomic, weak) UIScrollView *contentView;
@end

@implementation MLSegmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;

}
-(void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    if (self.segmentBar.superview == self.view) {
        self.segmentBar.frame = CGRectMake(0, 60, self.view.width, 50);
        CGFloat contentY = self.segmentBar.y + self.segmentBar.height;
        CGRect frame = CGRectMake(0, contentY, self.view.width, self.view.height - contentY);
        self.contentView.frame = frame;
        self.contentView.contentSize = CGSizeMake(self.childViewControllers.count *self.contentView.width, 0);
        return;
        
    }
    
    CGRect contentFrame = CGRectMake(0, 0,self.view.width,self.view.height);
    self.contentView.frame = contentFrame;
    self.contentView.contentSize = CGSizeMake(self.childViewControllers.count * self.view.width, 0);
    
    self.segmentBar.selectIndex = self.segmentBar.selectIndex;
}

-(MLSegmentBar *)segmentBar {
    if (!_segmentBar) {
        MLSegmentBar * segmentBar = [MLSegmentBar segmentBarWithFrame:CGRectZero];
        segmentBar.delegate = self;
        [self.view addSubview:segmentBar];
        _segmentBar = segmentBar;
    }
    return _segmentBar;
}
-(UIScrollView *)contentView {
    if (!_contentView) {
        UIScrollView *contentView = [[UIScrollView alloc] init];
        contentView.delegate = self;
        contentView.pagingEnabled = YES;
//        contentView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [self.view addSubview:contentView];
        _contentView = contentView;
        
    }
    return _contentView;
}

-(void)setupBarWithItems:(NSArray<NSString *> *)items childVcs:(NSArray<UIViewController *> *)childVcs {
    
    NSAssert(items.count != 0 || items.count == childVcs.count, @"个数不一致,请检查");
    self.segmentBar.items = items;
    //将之前添加的子控制器先移除
    [self.childViewControllers makeObjectsPerformSelector:@selector(removeFromParentViewController)];
    for (UIViewController *childVc in childVcs) {
        [self addChildViewController:childVc];
    }
    self.contentView.contentSize = CGSizeMake(items.count *self.view.width, 0);
    self.segmentBar.selectIndex = 0;
}

- (void)showChlidVcAtIndex:(NSInteger)index {
    if (self.childViewControllers.count == 0 || index < 0 ||index > self.childViewControllers.count - 1) {
        return;
    }
    UIViewController *vc = self.childViewControllers[index];
    vc.view.frame = CGRectMake(index * self.contentView.width, 0, self.contentView.width, self.contentView.height);
    [self.contentView addSubview:vc.view];
    
    [self.contentView setContentOffset:CGPointMake(index * self.contentView.width, 0) animated:YES];
}


- (void)segmentBar:(MLSegmentBar *)segmentBar didSelectIndex:(NSInteger)toIndex fromIndex:(NSInteger)fromIndex {
    NSLog(@"%zd,%zd",toIndex,fromIndex);
    [self showChlidVcAtIndex:toIndex];
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger index = self.contentView.contentOffset.x / self.contentView.width;
    self.segmentBar.selectIndex = index;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end



